<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="/X-admin/css/font.css">
    <link rel="stylesheet" href="/X-admin/css/login.css">
    <link rel="stylesheet" href="/X-admin/css/xadmin.css">
    <script src="/js/jquery.js"></script>
    <script src="/X-admin/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/X-admin/js/xadmin.js"></script>

    <script src="../../js/myAjax.js"></script>

</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form">

            <div class="layui-form-item">
                <label for="productName" class="layui-form-label">
                    <span class="x-red">*</span>产品名称</label>
                <div class="layui-input-inline">
                    <input type="text" id="productName" name="productName" lay-verify="username"
                           autocomplete="off" class="layui-input"></div>
            </div>
            <div class="layui-form-item">
                <label for="productName" class="layui-form-label">
                    <span class="x-red">*</span>产品价格</label>
                <div class="layui-input-inline">
                    <input type="text" id="price" name="price"
                           autocomplete="off" class="layui-input"></div>
            </div>
            <div class="layui-form-item">
                <label for="productName" class="layui-form-label">
                    <span class="x-red">*</span>状态</label>
                <div class="layui-input-inline">
                    <input type="radio" name="enable" value="1" title="启用">
                    <input type="radio" name="enable" value="0" title="停用">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="productName" class="layui-form-label">
                    <span class="x-red">*</span>图片</label>
                <div class="layui-input-inline">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="productImgHref">上传图片</button>
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="demo1" style="width: 200px;height:200px">
                            <p id="demoText"></p>
                        </div>
                    </div>

                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn" lay-filter="add" lay-submit="">增加</button>
            </div>
        </form>
    </div>
</div>
<script>layui.use(['upload', 'laydate', 'form', 'layer', 'jquery'],
    function () {
        $ = layui.jquery;
        let laydate = layui.laydate;
        let form = layui.form;
        let upload = layui.upload;
        let layer = layui.layer;

        //常规使用 - 普通图片上传
        let uploadInst = upload.render({
            elem: '#productImgHref'
            //改成您自己的上传接口,需要將图片上传到哪里
            //调用后台代码
            , url: '/upload'
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });

                // element.progress('demo', '0%'); //进度条复位
                // layer.msg('上传中', {icon: 16, time: 0});
            }
            , done: function (res) {
                console.log(res);
                sessionStorage.setItem("productImgHref", res.url);
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                //上传成功的一些操作
                //……

            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });

        //监听提交
        form.on('submit(add)',
            function (data) {

                data=data.field;
                data.imgHref=sessionStorage.getItem("productImgHref");
                console.log(data, 111)

                //发异步，把数据提交给java后台
                let result = myAjax("/back/product/add", data, "post");
               if (result.result == 'success') {
                    layer.alert("增加成功", {
                            icon: 6
                        },
                        function () {
                            //关闭当前frame
                            xadmin.close();

                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                        });
                } else {
                    layer.alert("增加失败");
                }
                return false;
            });

    });</script>


</body>

</html>